package yingke.com.yingke.ui.right;

import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import yingke.com.yingke.R;
import yingke.com.yingke.config.Constant;
import yingke.com.yingke.ui.NEVideoPlayerActivity;
import yingke.com.yingke.ui.right.bean.LiveNodesBean;

/**
 * Created by Administrator on 2016/10/22.
 */

public class SearchHeadAdapter extends BaseAdapter {

    Context mcontext;
    List<LiveNodesBean.LivesBean> mlist;

    public SearchHeadAdapter(Context context,List<LiveNodesBean.LivesBean> list) {
        mcontext = context;
        mlist = list;
    }

    @Override
    public int getCount() {
        return mlist!=null?mlist.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mlist!=null?mlist.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHodle hodle;
        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.search_head_item, parent, false);
            hodle = new ViewHodle(convertView);
            convertView.setTag(hodle);
        } else {
            hodle = (ViewHodle) convertView.getTag();
        }
        LiveNodesBean.LivesBean livesBean = mlist.get(position);
        String img = livesBean.getCreator().getPortrait();
        int num = livesBean.getOnline_users();

        //动态设置图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        ((SearchActivity)mcontext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels;
        ViewGroup.LayoutParams params = hodle.head_img.getLayoutParams();
        params.width=screenWidth/3;
        params.height=screenWidth/3;
        hodle.head_img.setLayoutParams(params);
        hodle.head_tx.setText(num+"人");


        Glide.with(mcontext).load(Constant.IMG_BASE_URL+img).into(hodle.head_img);
        hodle.head_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, NEVideoPlayerActivity.class);
                intent.putExtra("videoPath",mlist.get(position).getStream_addr());
                intent.putExtra("name",mlist.get(position).getName());
                mcontext.startActivity(intent);
            }
        });
        return convertView;
    }
    class ViewHodle{
        ImageView head_img;
        TextView head_tx;
        LinearLayout head_ll;
        public ViewHodle(View convertView) {
            head_img = (ImageView) convertView.findViewById(R.id.search_head_iv);
            head_tx = (TextView) convertView.findViewById(R.id.search_head_tv);
            head_ll = (LinearLayout) convertView.findViewById(R.id.search_head_item_ll);
        }
    }
}
